package com.feituan.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feituan.core.persistence.service.ICouponService;
import com.feituan.core.persistence.service.IMemberAmountService;
import com.feituan.core.persistence.service.IMemberCouponService;
import com.feituan.core.pojo.entity.Coupon;
import com.feituan.core.pojo.entity.MemberAmount;
import com.feituan.core.pojo.entity.MemberCoupon;
import com.feituan.core.pojo.request.MemberAmountReq;
import com.feituan.core.pojo.response.MemberCouponRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/memberAmount")
public class MemberAmountController {

    @Resource
    private IMemberAmountService amountService;

    @Resource
    private IMemberCouponService memberCouponService;

    /**
     *
     */
    @Resource
    private ICouponService couponService;


    /**
     * 新增/修改
     * @param memberAmountReq
     * @return
     */
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public Response<Boolean> save(@RequestBody MemberAmountReq memberAmountReq) {
        if (memberAmountReq.getId() == null) {
            MemberAmount memberAmount = new MemberAmount();
            memberAmount.setPrice(memberAmountReq.getPrice());
            memberAmount.setType(memberAmountReq.getType());
            amountService.getBaseMapper().insert(memberAmount);

            List<MemberCoupon> memberCouponList = memberAmountReq.getMemberCouponList();
            memberCouponList.forEach(el -> {
                el.setMemberAmountId(memberAmount.getId());
                memberCouponService.getBaseMapper().insert(el);
            });

            return Response.success(true);
        } else {
            MemberAmount memberAmount = new MemberAmount();
            memberAmount.setPrice(memberAmountReq.getPrice());
            memberAmount.setId(memberAmountReq.getId());
            amountService.getBaseMapper().updateById(memberAmount);

            List<MemberCoupon> memberCouponList = memberAmountReq.getMemberCouponList();
            for (MemberCoupon el : memberCouponList) {
                if (el.getId() == null) {
                    // 新增优惠券
                    el.setMemberAmountId(memberAmountReq.getId());
                    memberCouponService.getBaseMapper().insert(el);
                } else {
                    // 修改
                    memberCouponService.getBaseMapper().updateById(el);
                }
                List<Long> deleteCouponList = memberAmountReq.getDeleteCouponList();
                if (CollectionUtils.isEmpty(deleteCouponList)) {
                    continue;
                }
                deleteCouponList.forEach(it -> {
                    memberCouponService.getBaseMapper().deleteById(it);
                });
            }
            return Response.success(true);
        }

    }



    @PostMapping("/list_page")
    public Response<PageData<MemberAmountReq>> selectList(MemberAmountReq amountReq) {
        Page<MemberAmount> page = new Page<>();
        page.setCurrent(amountReq.getPage());
        page.setSize(amountReq.getLimit());

        LambdaQueryWrapper<MemberAmount> qw = new LambdaQueryWrapper<>();
        qw.eq(!ObjectUtils.isEmpty(amountReq.getType()), MemberAmount::getType, amountReq.getType());
        Page<MemberAmount> amountPage = amountService.getBaseMapper().selectPage(page, qw);
        List<MemberAmount> records = amountPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return Response.success(PageData.build(1, 0, null));
        }
        List<MemberAmountReq> memberAmountReqList = new ArrayList<>();
        records.forEach(el -> {
            MemberAmountReq memberAmountReq = new MemberAmountReq();
            BeanUtils.copyProperties(el, memberAmountReq);
            // 查询与之绑定的优惠卷
            LambdaQueryWrapper<MemberCoupon> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MemberCoupon::getMemberAmountId, el.getId());
            List<MemberCoupon> memberCoupons = memberCouponService.getBaseMapper().selectList(queryWrapper);
            List<MemberCouponRes> memberCouponResList = new ArrayList<>();
            memberCoupons.forEach(memberCoupon -> {
                MemberCouponRes memberCouponRes = new MemberCouponRes();
                BeanUtils.copyProperties(memberCoupon, memberCouponRes);
                Coupon coupon = couponService.getBaseMapper().selectById(memberCoupon.getCouponId());
                memberCouponRes.setCoupon(coupon);
                memberCouponResList.add(memberCouponRes);
            });
            memberAmountReq.setCouponList(memberCouponResList);
            memberAmountReqList.add(memberAmountReq);
        });
        return Response.success(PageData.build(amountPage.getCurrent(), amountPage.getTotal(), memberAmountReqList));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Response<Boolean> delete(Long id) {
        int i = amountService.getBaseMapper().deleteById(id);
        return Response.success(i > 0);
    }
}
